using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using System;
using System.Collections.Generic;
namespace MicroMsg.Network
{
	public class Connector
	{
		public class ConnectorContext
		{
			public int mConnectMode = 1;
			public int mSeq;
			public int mCmd;
			public string mCmdUri;
			public HttpClient mHttpClient;
		}
		public class RawPackBody
		{
			public int int_0;
			public string cmdUri;
			public int int_1;
			public byte[] body;
		}
		public const int CONN_MODE_NULL = 0;
		public const int CONN_MODE_TCP = 1;
		public const int CONN_MODE_HTTP = 2;
		public const int CONN_MODE_ALL = 3;
		public static double mLastActiveTimestamp = 0.0;
		private static List<Connector.RawPackBody> mListRecvPack = new List<Connector.RawPackBody>();
		private static ConntectorStatus mConnectorStatus = ConntectorStatus.Disconnect;
		public static double mLastShortKeepAliveTimestamp = 0.0;
		public static void onPrepareSend(bool needCheckAlive)
		{
			NetHandler.startup();
			LongConnector.onPrepare();
			ShortConnector.onPrepare();
			if (needCheckAlive)
			{
				Connector.checkNeedKeepAliveInShortConn(0.0);
			}
		}
		public static bool isValidforPack(SessionPack sessionPack)
		{
			if (!LongConnector.isAvailable())
			{
				sessionPack.mConnectMode = 2;
				sessionPack.mCacheBodyBuffer = null;
			}
			int mConnectMode = sessionPack.mConnectMode;
			if (mConnectMode == 1)
			{
				return LongConnector.isValid();
			}
			if (mConnectMode != 2)
			{
				return false;
			}
			if (string.IsNullOrEmpty(sessionPack.mCmdUri))
			{
				Log.smethod_1("Network", "Cannot send pack via http without uri,  will been cancelled. seq= " + sessionPack.mSeqID);
				sessionPack.mCanceled = true;
				return false;
			}
			return ShortConnector.isValid();
		}
		private static void checkNeedKeepAliveInShortConn(double interval)
		{
			if (!NetworkDeviceWatcher.isNetworkAvailable())
			{
				return;
			}
			if (LongConnector.isAvailable())
			{
				return;
			}
			if (!ShortConnector.isValid())
			{
				return;
			}
			double nowMilliseconds = Util.getNowMilliseconds();
			if (Connector.mLastShortKeepAliveTimestamp == 0.0)
			{
				Connector.mLastShortKeepAliveTimestamp = nowMilliseconds;
			}
			if (nowMilliseconds >= Connector.mLastShortKeepAliveTimestamp + interval)
			{
				Log.smethod_4("Network", "need keep alive in short connnector mode, post event to sync.");
				EventCenter.postEvent(EventConst.ON_NETSCENE_NOTIFY_SYNC_REQ, null, null);
				Connector.mLastShortKeepAliveTimestamp = nowMilliseconds;
			}
		}
		public static void printInfo()
		{
			NetworkDeviceWatcher.printfInfo();
			Log.smethod_4("Network", string.Concat(new object[]
			{
				"connector status = ",
				Connector.mConnectorStatus,
				", network available = ",
				NetworkDeviceWatcher.isNetworkAvailable(),
				", longconn available = ",
				LongConnector.isAvailable()
			}));
		}
		public static void checkReady()
		{
			Connector.checkConnectStatus();
			Connector.checkNeedKeepAliveInShortConn(60000.0);
			LongConnector.checkReady();
			ShortConnector.checkReady();
		}
		private static void checkConnectStatus()
		{
			if (!NetworkDeviceWatcher.isNetworkAvailable())
			{
				Connector.setConnectorStatus(ConntectorStatus.Disconnect);
				return;
			}
			if (LongConnector.isAvailable() && Connector.mConnectorStatus != ConntectorStatus.Disconnect)
			{
				return;
			}
			if (ShortConnector.isAvailable())
			{
				Connector.setConnectorStatus(ConntectorStatus.Connected);
				return;
			}
			Connector.setConnectorStatus(ConntectorStatus.Disconnect);
		}
		public static void close()
		{
			Log.smethod_4("Network", "close connector. ");
			LongConnector.close();
			ShortConnector.close();
			Connector.onPrepareSend(false);
		}
		public static bool sendPack(SessionPack sessionPack)
		{
			Connector.mLastActiveTimestamp = SessionPack.getNow();
			int mConnectMode = sessionPack.mConnectMode;
			if (mConnectMode == 1)
			{
				return LongConnector.sendPack(sessionPack);
			}
			return mConnectMode != 2 || ShortConnector.sendPack(sessionPack);
		}
		public static void onSendFailed(bool needReconnect, int connMode)
		{
			if ((connMode & 1) != 0)
			{
				LongConnector.onSendFailed(needReconnect);
			}
			if ((connMode & 2) != 0)
			{
				ShortConnector.onSendFailed(needReconnect);
			}
		}
		public static List<Connector.RawPackBody> receiveBodyList()
		{
			Connector.mListRecvPack.Clear();
			LongConnector.receive(Connector.mListRecvPack);
			ShortConnector.receiveList(Connector.mListRecvPack);
			if (Connector.mListRecvPack.get_Count() > 0)
			{
				Connector.mLastActiveTimestamp = SessionPack.getNow();
			}
			return Connector.mListRecvPack;
		}
		public static ConntectorStatus getConnectorStatus()
		{
			return Connector.mConnectorStatus;
		}
		public static void setConnectorStatus(ConntectorStatus status)
		{
			if (status == Connector.mConnectorStatus)
			{
				return;
			}
			Connector.mConnectorStatus = status;
			if (status == ConntectorStatus.Connected)
			{
				EventCenter.postEvent(EventConst.ON_NET_MM_NET_CONNECTED, null, null);
				return;
			}
			if (status == ConntectorStatus.Connecting)
			{
				EventCenter.postEvent(EventConst.ON_NET_MM_NET_CONNECTING, null, null);
				return;
			}
			if (status == ConntectorStatus.Disconnect)
			{
				EventCenter.postEvent(EventConst.ON_NET_MM_NET_DISCONNECT, null, null);
			}
		}
	}
}
